package com.ruikong.dazhi.service.impl.baseinfo;


import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.dao.basicinfo.ShopMapper;
import com.ruikong.dazhi.dao.basicinfo.ShopTypeMapper;
import com.ruikong.dazhi.entity.basicinfo.ShopType;
import com.ruikong.dazhi.model.output.TreeMenuOutput;
import com.ruikong.dazhi.service.basicinfo.IShopTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (ShopType)表服务实现类
 *
 * @author HuaJiaYi
 * @since 2021-01-19 17:44:33
 */
@Service("shopTypeService")
public class ShopTypeServiceImpl implements IShopTypeService {
    @Autowired
    private ShopTypeMapper shopTypeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public ShopType queryById(Integer id) {
        return this.shopTypeMapper.queryById(id);
    }

//    @Override
//    public List<ShopType> queryByPId(Integer id){
//        return this.shopTypeMapper.queryByPId(id);
//    }

    @Override
    public Integer insert(ShopType shopType) {
            return this.shopTypeMapper.insert(shopType);
    }

    @Override
    public Integer update(ShopType shopType) {
        return this.shopTypeMapper.update(shopType);
    }

    @Override
    public BaseResult deleteById(Integer id) {
        Integer shopCount = shopMapper.queryByShopTypeId(id);
        BaseResult baseResult=new BaseResult();
        if (shopCount > 0){
            baseResult.setMsg("要删除的车间类别存在车间，不允许被删除");
            baseResult.setCode(ResultApi.failCode);
        }
        else{
            this.shopTypeMapper.deleteById(id);
            baseResult.setCode(ResultApi.successCode);
            baseResult.setMsg(ResultApi.deleteFailMsg);
        }
        return baseResult;
    }

    @Override
    public List<TreeMenuOutput> queryAllShopTypeTree(){
        List<TreeMenuOutput> treeMenuOutput = new ArrayList<>();
        TreeMenuOutput root = new TreeMenuOutput();
        root.setId(0);
        root.setName("全部");
        root.setpId(-1);
        treeMenuOutput.add(root);

        List<ShopType> listShopType = shopTypeMapper.queryAllShopTypeTree();
        for (int i = 0;i<listShopType.size();i++){
            ShopType shoptype = listShopType.get(i);
            TreeMenuOutput treeMenuOutput1 = new TreeMenuOutput();
            treeMenuOutput1.setId(shoptype.getId());
            treeMenuOutput1.setName(shoptype.getShopTypeName());
            treeMenuOutput1.setpId(0);
            treeMenuOutput.add(treeMenuOutput1);
        }
        return treeMenuOutput;
    }

    @Override
    public Integer isExist(int id,String shopTypeName){
        return this.shopTypeMapper.isExist(id,shopTypeName);
    }
}